package cn.jy.task.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.task.domain.Commodityinfo;
import cn.jy.task.domain.Customer;
import cn.jy.task.domain.Taskinfo;
import cn.jy.task.mapper.CommodityinfoMapper;
import cn.jy.task.mapper.CustomerMapper;
import cn.jy.task.mapper.TaskinfoMapper;
import cn.jy.task.service.IPurchaseAndSellService;
@Service
public class PurchaseAndSellServiceImpl implements IPurchaseAndSellService {
	
	  @Autowired
	  TaskinfoMapper  taskinfoMapper;
	  @Autowired
	  CommodityinfoMapper commodityinfoMapper;
	  @Autowired
	  CustomerMapper customerMapper;
	  Logger logger=Logger.getLogger(PurchaseAndSellServiceImpl.class);
	@Override
	public void purchase() {
	    //先去查询到数据库中 满足条件的数据  （到货未支付货款供应商列表）
		 List<Taskinfo> selectPurchaseList = taskinfoMapper.selectCustomerList("rwlx='1' and hwzt=1 and hkzt=0");
		 for (Taskinfo taskinfo : selectPurchaseList) {
			 //查询到这个供应商一共有几条采购订单
			 List<Taskinfo> TaskinfoList = taskinfoMapper.selectByCondition("rwlx='1' and hwzt=1 and hkzt=0 and khxx= '"+taskinfo.getKhxx()+"'" );
			 logger.info(taskinfo.getKhxx()+"---"+TaskinfoList.size()+"条");
			 double sum=0.0;
			 for (Taskinfo taskinfo2 : TaskinfoList) {
				 List<Commodityinfo> CommodityinfoList = commodityinfoMapper.selectBycondition("tid='"+taskinfo2.getTid()+"'");
				   for (Commodityinfo commodityinfo : CommodityinfoList) {
					   if (commodityinfo.getSpsjzj()!=null) {
						   sum=sum+commodityinfo.getSpsjzj();
					}
				}
			}
			 logger.info(taskinfo.getKhxx()+"---"+sum);
			 //根据客户是信息taskinfo.getKhxx()去更改他的应付款
			 Customer customer = customerMapper.selectByCondition("CODE='"+taskinfo.getKhxx()+"'");
			 customer.setPaysum(sum);
			 logger.info(customer.getName()+"---"+customer.getPaysum());
			 customerMapper.updateByPrimaryKey(customer);
		}
		// 先去查询到数据库中 满足条件的数据  （到货未收到发票供应商列表）
		 List<Taskinfo> invoiceList = taskinfoMapper.selectCustomerList("rwlx='1' and hwzt=1 and fpzt=0"); 
		 for (Taskinfo taskinfo : invoiceList) {
			//查询到这个供应商一共有几条采购订单
			 List<Taskinfo> TaskinfoList = taskinfoMapper.selectByCondition("rwlx='1' and hwzt=1 and fpzt=0 and khxx= '"+taskinfo.getKhxx()+"'" );
			 logger.info(taskinfo.getKhxx()+"---"+TaskinfoList.size()+"条");
			 double sum=0.0;
			 for (Taskinfo taskinfo2 : TaskinfoList) {
				 List<Commodityinfo> CommodityinfoList = commodityinfoMapper.selectBycondition("tid='"+taskinfo2.getTid()+"'");
				   for (Commodityinfo commodityinfo : CommodityinfoList) {
					   if (commodityinfo.getSpsjzj()!=null) {
						   sum=sum+commodityinfo.getSpsjzj();
					}
				}
			}
			 logger.info(taskinfo.getKhxx()+"---"+sum);
			 Customer customer = customerMapper.selectByCondition("CODE='"+taskinfo.getKhxx()+"'");
			 customer.setInvoicesum(sum);;
			 logger.info(customer.getName()+"---"+customer.getInvoicesum()+"欠票");
			 customerMapper.updateByPrimaryKey(customer);
		}
		 
	}

	@Override
	public void sell() {
		  //先去查询到数据库中 满足条件的数据  （到货未支付货款供应商列表）
		 List<Taskinfo> selectPurchaseList = taskinfoMapper.selectCustomerList("rwlx='2' and hwzt=1 and hkzt=0");
		 for (Taskinfo taskinfo : selectPurchaseList) {
			 //查询到这个供应商一共有几条采购订单
			 List<Taskinfo> TaskinfoList = taskinfoMapper.selectByCondition("rwlx='2' and hwzt=1 and hkzt=0 and khxx= '"+taskinfo.getKhxx()+"'" );
			 logger.info(taskinfo.getKhxx()+"---"+TaskinfoList.size()+"条");
			 double sum=0.0;
			 for (Taskinfo taskinfo2 : TaskinfoList) {
				 List<Commodityinfo> CommodityinfoList = commodityinfoMapper.selectBycondition("tid='"+taskinfo2.getTid()+"'");
				   for (Commodityinfo commodityinfo : CommodityinfoList) {
					   if (commodityinfo.getSpsjzj()!=null) {
						   sum=sum+commodityinfo.getSpsjzj();
					}
				}
			}
			 logger.info(taskinfo.getKhxx()+"---"+sum);
			 //根据客户是信息taskinfo.getKhxx()去更改他的应付款
			 Customer customer = customerMapper.selectByCondition("CODE='"+taskinfo.getKhxx()+"'");
			 customer.setPaysum(sum);
			 logger.info(customer.getName()+"---"+customer.getPaysum()+"未收到货款");
			 customerMapper.updateByPrimaryKey(customer);
		}
		// 先去查询到数据库中 满足条件的数据  （到货未收到发票供应商列表）
		 List<Taskinfo> invoiceList = taskinfoMapper.selectCustomerList("rwlx='2' and hwzt=1 and fpzt=0"); 
		 for (Taskinfo taskinfo : invoiceList) {
			//查询到这个供应商一共有几条采购订单
			 List<Taskinfo> TaskinfoList = taskinfoMapper.selectByCondition("rwlx='2' and hwzt=1 and fpzt=0 and khxx= '"+taskinfo.getKhxx()+"'" );
			 logger.info(taskinfo.getKhxx()+"---"+TaskinfoList.size()+"条");
			 double sum=0.0;
			 for (Taskinfo taskinfo2 : TaskinfoList) {
				 List<Commodityinfo> CommodityinfoList = commodityinfoMapper.selectBycondition("tid='"+taskinfo2.getTid()+"'");
				   for (Commodityinfo commodityinfo : CommodityinfoList) {
					   if (commodityinfo.getSpsjzj()!=null) {
						   sum=sum+commodityinfo.getSpsjzj();
					}
				}
			}
			 logger.info(taskinfo.getKhxx()+"---"+sum);
			 Customer customer = customerMapper.selectByCondition("CODE='"+taskinfo.getKhxx()+"'");
			 customer.setInvoicesum(sum);;
			 logger.info(customer.getName()+"---"+customer.getInvoicesum()+"欠对方票");
			 customerMapper.updateByPrimaryKey(customer);
		}
	}

}
